package com.zbw.controller;


import com.zbw.domain.Appoint;
import com.zbw.domain.Book;
import com.zbw.domain.BorrowingBooks;
import com.zbw.domain.User;
import com.zbw.domain.Vo.BookVo;
import com.zbw.service.IAppointService;
import com.zbw.service.IBookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 预约
 */
@Controller
public class AppointController {

    @Resource
    private IAppointService appointService;

    @Resource
    private IBookService bookService;

    /**
     * 返回用户索书页面
     */
    @RequestMapping("/findAppoint")
    public String findBookPage() {
        return "user/appointBook";
    }


    @RequestMapping("/findAppointPartInfo")
    public String findAppointPartInfo(@RequestParam("bookPartInfo") String bookPartInfo, Model model, HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("user");
        List<BookVo> bookVos = appointService.findAppointPartInfo(bookPartInfo, "");

        model.addAttribute("bookList", bookVos);
        return "user/appointBook";
    }

    @RequestMapping("/selectTimeBook")
    public String selectTimeBook(Model model, HttpServletRequest request) {
        List<BookVo> bookVos = appointService.selectKeYuYueBooks();
        model.addAttribute("bookList", bookVos);

        return "user/appointBook";
    }


    //appoint/book预约
    @RequestMapping("/appoint/book")
    @ResponseBody
    public boolean updateBooks(BorrowingBooks books, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        Appoint appoint = new Appoint();
        appoint.setUsername(user.getUserName());
        appoint.setBookId(books.getBookId());

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        appoint.setDate(format.format(date));

        //先查询是否有这个书
        Book book = bookService.selectById(books.getBookId());
        if (ObjectUtils.isEmpty(book)) {
            return false;
        }
        //在查询这个书是否被预约了
        List<BookVo> byBookId = appointService.findByBookId(book.getBookId(), null);
        if (!CollectionUtils.isEmpty(byBookId)) {
            return false;
        }


        List<BookVo> appointPartInfo = appointService.findByBookId(books.getBookId(), user.getUserName());
        if (CollectionUtils.isEmpty(appointPartInfo)) {
            return appointService.insert(appoint);
        } else {
            return appointService.update(appoint);
        }
    }


    /**
     * 所有人预约列表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/appointBookAll")
    public String appointBookAll(Model model, HttpServletRequest request) {
        List<BookVo> bookVos = appointService.findByBookId(null, null);
        model.addAttribute("bookList", bookVos);

        return "admin/appointBook";
    }


    /**
     * 取消预约
     *
     * @param books
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("cancalBook")
    @ResponseBody
    public boolean cancalBook(BorrowingBooks books, Model model, HttpServletRequest request) {
        appointService.deleteBook(books.getBookId());
        return true;
    }

}
